package cn.fan.vertd.verticle;

import cn.fan.vertd.utils.RedisUtil;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.redis.client.RedisConnection;

public class RedisOptVerticle extends AbstractVerticle {

    private static RedisConnection redisConnection;

    @Override
    public void start(Promise<Void> startPromise) throws Exception {
        init(config())
                .onSuccess(startPromise::complete)
                .onFailure(startPromise::fail);
        Router router = Router.router(vertx);
        router.get("/api/v1/test").handler(this::testHandle);
        vertx.createHttpServer()
                .requestHandler(router)
                .listen(8080);
    }
    private void testHandle(RoutingContext ctx) {
        ctx.response().setStatusCode(200)
                .setStatusMessage("OK")
                .putHeader("Content-Type","text/plain")
                .end("test");
    }

    @Override
    public void stop(Promise<Void> stopPromise) throws Exception {
        super.stop(stopPromise);
    }

    private Future<Void> init(JsonObject conf) {
        Promise<Void> promise = Promise.promise();
        RedisUtil.init(vertx, conf);
        promise.complete();
        return promise.future();
    }


}
